/*
 * Copyright 2013-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ccblife.third.msagent.msagentfeign.rpc;


import com.ccblife.third.msagent.msagentfeign.login.LoginParam;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;
import java.util.Set;


class MsagentClientsRegistrar implements ImportBeanDefinitionRegistrar , ApplicationContextAware , EnvironmentAware {


   public   static Environment environment;

   public   static ApplicationContext applicationContext;

   MsagentClientsRegistrar() {
	}


	@Override
	public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
		registerMsagentClients(metadata, registry);
	}


	public void registerMsagentClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
		Set<Class<?>> set = null;
		try {
			//获取EnableMsagent注解中配置的扫描包路径
			Map<String, Object> defaultAttrs = metadata.getAnnotationAttributes(EnableMsagent.class.getName(), true);
			String packages=(String)defaultAttrs.get("value");
			String[] packageList= packages.split(",");
			for(String packag:packageList){
				//获取配置了MsagentClient的类
			set = new Scanner().getAnnotationClasses(packag, MsagentClient.class);
			for(Class beanClazz:set){
				//将工厂类注入IOC容器管理
				BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClazz);
				GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
				definition.getConstructorArgumentValues().addGenericArgumentValue(beanClazz);
				definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
				definition.setBeanClass(MsagentClientFactoryBean.class);
				registry.registerBeanDefinition(beanClazz.getSimpleName(), definition);
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}


	@Override
	public void setEnvironment(Environment environment) {
		this.environment=environment;
		LoginParam loginparam=LoginParam.getInstance();
		loginparam.setLoginpassword(environment.getProperty("msagent.loginParam.loginpassword"));
		loginparam.refresh();
	}
}



